// pages/weather/weather.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
     city:"",//城市
     temptu:"",//温度
     text:"",//天气文字
    air:"",//空气质量
icon:""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },
  onShow:function(){
   
    this.getweather();
  },
  getweather(){
    var that=this;
    wx.getLocation({
      type: 'wgs84',
      success (res) {
        const latitude = res.latitude
        const longitude = res.longitude
       const key='7edc6a09b3e246a6b78d9389d7b8e1b0';
        wx.request({
          url: `https://devapi.qweather.com/v7/weather/now?location=${longitude},${latitude}&key=${key}`, 
          success (res) {
            console.log(res.data)
             that.setData({
              temptu:res.data.now.temp,
              text:res.data.now.text,
              icon:"/images/"+res.data.now.icon+".png"
             })
          }
        })
        wx.request({
          url: `https://geoapi.qweather.com/v2/city/lookup?location=${longitude},${latitude}&key=${key}`,
          success(resm){
            that.setData({
              city:resm.data.location[0].adm1+"-"+resm.data.location[0].name
            })
          }
        })
        wx.request({
          url: `https://devapi.qweather.com/v7/air/now?location=${longitude},${latitude}&key=${key}`,
          success(resn){
            that.setData({
             air:resn.data.now.category
            })
          }
        })
      }
     })
  },
  onPullDownRefresh(){
    this.getweather();
  }
 
})